/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.linktco.prison.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.linktco.common.utils.U;
import com.linktco.prison.entity.CabinetCell;
import com.linktco.prison.entity.CabinetInfo;
import com.linktco.prison.entity.CabinetUser;
import com.linktco.prison.entity.CabinetCellUser;
import com.linktco.prison.mapper.CabinetCellUserMapper;
import com.linktco.prison.service.ICabinetCellService;
import com.linktco.prison.service.ICabinetCellUserService;
import com.linktco.prison.service.ICabinetInfoService;
import com.linktco.prison.vo.CabinetCellUserVO;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.List;

/**
 * 档案柜格 服务实现类
 *
 * @author BladeX
 * @since 2024-12-23
 */
@Service
public class CabinetCellUserServiceImpl extends BaseServiceImpl<CabinetCellUserMapper, CabinetCellUser> implements ICabinetCellUserService {


    @Autowired
    private ICabinetCellService cellService;
    @Autowired
    private ICabinetInfoService cabinetService;

    @Override
    public IPage<CabinetCellUserVO> selectCabinetCellUserPage(IPage<CabinetCellUserVO> page, CabinetCellUserVO cellUser) {
        return page.setRecords(baseMapper.selectCabinetCellUserPage(page, cellUser));
    }

    @Override
    public List<CabinetUser> getUserList(Long cellId) {
        return baseMapper.getUserList(cellId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean customSave(Long cellId, List<Long> userIds) {

        CabinetCell cell = cellService.getById(cellId);
        CabinetInfo cabinet = cabinetService.getById(cell.getCabinetId());

        long count = count(Wrappers.<CabinetCellUser>lambdaQuery().eq(CabinetCellUser::getCellId, cellId));
        Assert.isTrue(count + userIds.size() <= cabinet.getCellUsers(),
                "每格最多只能放置" + cabinet.getCellUsers() + "人档案！");

        List<CabinetCellUser> cellUserList = new ArrayList<>();
        userIds.forEach(userId -> cellUserList.add(builderCellUser(cell, userId)));
        return saveOrUpdateBatch(cellUserList);
    }

    private CabinetCellUser builderCellUser(CabinetCell cell, Long userId) {
        CabinetCellUser user = getOne(Wrappers.<CabinetCellUser>lambdaQuery()
                .eq(CabinetCellUser::getCellId, cell.getId())
                .eq(CabinetCellUser::getUserId, userId));
        if (U.notEmpty(user)) {
            return user;
        }
        return CabinetCellUser.builder()
                .cabinetId(cell.getCabinetId())
                .cellId(cell.getId())
                .userId(userId).build();
    }


}
